package cn.xspace.module.other.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.ResourceUtils;

import java.util.List;
import java.util.Map;

import cn.xspace.module.other.R;
import cn.xspace.module.other.base.CleanCacheBean;

/**
 * Wubj 创建于 2017/3/8 0008.
 */
public class CleanCacheAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<CleanCacheBean> parentList;
    private Map<String, List<CleanCacheBean>> childrenList;

    public CleanCacheAdapter(Context mContext, List<CleanCacheBean> parentList, Map<String, List<CleanCacheBean>>
            childrenList) {
        this.mContext = mContext;
        this.parentList = parentList;
        this.childrenList = childrenList;
    }

    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<CleanCacheBean> stringList = childrenList.get(parentList.get(groupPosition).name);
        if (stringList != null) {
            return stringList.size();
        }
        return 0;
    }

    @Override
    public CleanCacheBean getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public CleanCacheBean getChild(int groupPosition, int childPosition) {
        return childrenList.get(parentList.get(groupPosition).name).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final CleanCacheBean bean = getGroup(groupPosition);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.clean_cache_list_item, null);
        }
        ImageView imageLeft = (ImageView) convertView.findViewById(R.id.iv_left_image);
        TextView type = (TextView) convertView.findViewById(R.id.tv_left_text);
        TextView size = (TextView) convertView.findViewById(R.id.tv_right_text);
        imageLeft.setImageResource(bean.ResId);
        type.setText(bean.name);
        size.setText(bean.sizeDesc);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCacheClick(bean);
                }
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup
            parent) {
        final CleanCacheBean bean = getChild(groupPosition, childPosition);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.clean_cache_list_item, null);
        }
        ImageView imageLeft = (ImageView) convertView.findViewById(R.id.iv_left_image);
        TextView type = (TextView) convertView.findViewById(R.id.tv_left_text);
        TextView size = (TextView) convertView.findViewById(R.id.tv_right_text);
        imageLeft.setVisibility(View.INVISIBLE);
        type.setText(String.format(ResourceUtils.getString(R.string.clean_string_suffix), bean.name));
        size.setText(bean.sizeDesc);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCacheClick(bean);
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private onCacheItemClickListener listener;

    public void setCacheItemClickListener(onCacheItemClickListener listener) {
        this.listener = listener;
    }

    public interface onCacheItemClickListener {
        void onCacheClick(CleanCacheBean bean);
    }


}
